package com.browan.constellation.weibo;

import com.browan.constellation.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class LoginWeiBo extends Activity
{

	public static final int MSG_LOGIN_SUCCESS = 0;
	public static final int MSG_LOGIN_ERROR = 1;

	private EditText m_UserNameEditText;
	private EditText m_PasswordEditText;

	private ProgressDialog m_LoginDialog;

	private SharedPreferences sharedPreferences;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sina_weibo_login);
		addListenece(findViewById(R.id.ok));
		addListenece(findViewById(R.id.cancel));
		m_UserNameEditText = (EditText) findViewById(R.id.username);
		m_PasswordEditText = (EditText) findViewById(R.id.password);

		m_LoginDialog = new ProgressDialog(this);
		m_LoginDialog.setCancelable(false);
		m_LoginDialog.setTitle("用户登录");
		m_LoginDialog.setMessage("登陆中........");
	}

	private void addListenece(View view)
	{

		view.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				switch (v.getId())
				{
				case R.id.ok:
					m_LoginDialog.show();
					new Thread(login).start();
					break;

				case R.id.cancel:
					LoginWeiBo.this.finish();
					break;

				default:
					break;
				}

			}
		});
	}

	Runnable login = new Runnable()
	{

		@Override
		public void run()
		{
			try
			{
				TockenManager.getInstence().initTocken();

				boolean isSuccess = Login.doLogin(m_UserNameEditText.getText().toString().trim(), m_PasswordEditText
						.getText().toString().trim());

				if (isSuccess)
				{

					handler.sendEmptyMessage(MSG_LOGIN_SUCCESS);
				}
				else
				{
					handler.sendEmptyMessage(MSG_LOGIN_ERROR);
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
				handler.sendEmptyMessage(MSG_LOGIN_ERROR);
			}

		}
	};

	Handler handler = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
			super.handleMessage(msg);

			m_LoginDialog.dismiss();
			switch (msg.what)
			{
			case MSG_LOGIN_ERROR:
				Toast.makeText(LoginWeiBo.this, "登录失败！", Toast.LENGTH_SHORT).show();
				break;

			case MSG_LOGIN_SUCCESS:

				String login_name = m_UserNameEditText.getText().toString().trim();

				String login_password = m_PasswordEditText.getText().toString().trim();

				sharedPreferences = getSharedPreferences(Configuration.SINA_WEIBO_FILE, MODE_PRIVATE);
				Editor editor = sharedPreferences.edit();

				editor.putString(Configuration.LOGIN_NAME, login_name);

				editor.putString(Configuration.LOGIN_PASSWORD, login_password);

				editor.commit();

				Intent intent = new Intent(LoginWeiBo.this, PostData.class);

				intent.putExtra(Intent.EXTRA_SUBJECT, getIntent().getStringExtra(Intent.EXTRA_SUBJECT));
				intent.putExtra(Intent.EXTRA_TEXT, getIntent().getStringExtra(Intent.EXTRA_TEXT));

				LoginWeiBo.this.startActivity(intent);
				LoginWeiBo.this.finish();
				break;
			default:
				break;
			}

		};
	};

}